package repository

import (
	"context"
	"github.com/starmotioncf/common/constant"
	"github.com/starmotioncf/common/db/field"
	"github.com/starmotioncf/common/restriction"
	"github.com/starmotioncf/soutchatinternal/model"
	"time"
)

type RestrictionRepository interface {
	// AddUidToBlackList uid添加到黑名单
	AddUidToBlackList(ctx context.Context, uid uint64, opts ...RestrictionOpt) (*model.Restriction, error)
	// AddIpToBlackList IP添加到黑名单
	AddIpToBlackList(ctx context.Context, ip string, opts ...RestrictionOpt) (*model.Restriction, error)
	// AddMacToBlackList Mac添加到黑名单
	AddMacToBlackList(ctx context.Context, mac string, opts ...RestrictionOpt) (*model.Restriction, error)
	// AddUidToWhiteList uid添加到白名单
	AddUidToWhiteList(ctx context.Context, uid uint64, opts ...RestrictionOpt) (*model.Restriction, error)
	// AddIpToWhiteList uid添加到白名单
	AddIpToWhiteList(ctx context.Context, ip string, opts ...RestrictionOpt) (*model.Restriction, error)
	// AddMacToWhiteList uid添加到白名单
	AddMacToWhiteList(ctx context.Context, mac string, opts ...RestrictionOpt) (*model.Restriction, error)
	// AddBlackList 添加黑名单
	AddBlackList(ctx context.Context, typ restriction.Typ, val string, opts ...RestrictionOpt) (*model.Restriction, error)
	// AddWhiteList 添加白名单
	AddWhiteList(ctx context.Context, typ restriction.Typ, val string, opts ...RestrictionOpt) (*model.Restriction, error)
	// AddRecord 添加名单记录
	AddRecord(ctx context.Context, mode restriction.Mode, typ restriction.Typ, val string, opts ...RestrictionOpt) (*model.Restriction, error)
	// GetRecord 获取一条名单记录
	GetRecord(ctx context.Context, typ restriction.Typ, val string) *model.Restriction
	// GetById 获取一条名单记录
	GetById(ctx context.Context, id int64) (*model.RestrictionRecord, error)
	// RemoveRecord 移除一个记录
	RemoveRecord(ctx context.Context, id int64, updateBy uint64) (err error)
}

type RestrictionOpt func(record *model.RestrictionRecord)

// RestrictionWithOperatorUid 关联uid
func RestrictionWithOperatorUid(uid uint64) RestrictionOpt {
	return func(record *model.RestrictionRecord) {
		record.OperatorUID = uid
	}
}

// RestrictionWithCreateBy 创建人
func RestrictionWithCreateBy(createBy int64) RestrictionOpt {
	return func(record *model.RestrictionRecord) {
		record.CreateBy = createBy
	}
}

// RestrictionWithUpdateBy 更新人
func RestrictionWithUpdateBy(updateBy int64) RestrictionOpt {
	return func(record *model.RestrictionRecord) {
		record.UpdateBy = updateBy
	}
}

// RestrictionWithTargetUid 目标uid
func RestrictionWithTargetUid(uid uint64) RestrictionOpt {
	return func(record *model.RestrictionRecord) {
		record.TargetUID = uid
	}
}

// RestrictionWithPromptMsg 提示消息
func RestrictionWithPromptMsg(promptMsg string) RestrictionOpt {
	return func(record *model.RestrictionRecord) {
		record.PromptMsg = promptMsg
	}
}

// RestrictionWithRemark 备注
func RestrictionWithRemark(remark string) RestrictionOpt {
	return func(record *model.RestrictionRecord) {
		record.Remark = remark
	}
}

// RestrictionWithImages 图片
func RestrictionWithImages(images []string) RestrictionOpt {
	return func(record *model.RestrictionRecord) {
		if len(images) > 0 {
			record.Images = field.JSONType[[]string]{images}
		}
	}
}

// RestrictionWithForever 永久
func RestrictionWithForever() RestrictionOpt {
	return func(record *model.RestrictionRecord) {
		record.Deadline = constant.Permanent
	}
}

// RestrictionWithAddTime 有效期
func RestrictionWithAddTime(sec int64) RestrictionOpt {
	return func(record *model.RestrictionRecord) {
		if sec > 0 {
			record.AddTime = sec
			record.Deadline = time.Now().Unix() + sec
		}
	}
}

// RestrictionWithBehavior 行为
func RestrictionWithBehavior(bs restriction.Behaviors) RestrictionOpt {
	return func(record *model.RestrictionRecord) {
		record.Behaviors = bs
	}
}

// RestrictionWithReasonType 原因
func RestrictionWithReasonType(rt restriction.ReasonType) RestrictionOpt {
	return func(record *model.RestrictionRecord) {
		record.ReasonType = rt
	}
}
